<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/10
 * Time: 21:24
 * 商品
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Queue;
use app\common\library\Redis;
use app\common\model\GoodsSpec;
use app\common\model\OrderGroup;
use think\Db;

class Goods extends Api {
	protected $noNeedLogin = ['index', 'detail', 'recommend'];
	
	/**
	 * 首页商品
	 */
	public function index() {
		$model = new \app\common\model\Goods();
		$where['status'] = $model::STATUS_SALE;
		$where['area'] = ['in', [$this->area, $model::AREA_NULL]];
		
		$keyword = $this->request->post('keyword');
		$keyword && $where['name'] = ['like', '%'.$keyword.'%'];
		
		$cateId = $this->request->post('cate_id');
		$cateId && $where['category_id'] = $cateId;
		
		$page = $this->request->post('page', 1, 'intval');
		
		$count = $model->where($where)->count();
		$rows = Db::name('goods')->where($where)
			->order(['sort' => 'asc'])
			->page($page)
			->field('id, name, image, images, price, number_sale')
			->select();
		
		if($rows) {
			foreach ($rows as &$row) {
				$images =array_filter(explode(',', $row['images']));
				$images[] = $row['image'];
				$row['images'] = $images;
			}
		}
		
		$this->success(__('Select success'), [
			'list' => $rows,
			'page' => $page,
			'total' => $count
		]);
	}
	
	
	/**
	 * 首页商品
	 */
	public function recommend() {
		$model = new \app\common\model\Goods();
		$where['status'] = $model::STATUS_SALE;
		$where['area'] = $this->area;
		$rows = Db::name('goods')->where($where)
			->order(['sort' => 'asc'])
			->limit(20)
			->field('id, name, image, price, number_sale')
			->select();
		
		$this->success(__('Select success'), $rows);
	}
	
	
	/**
	 * 详情
	 */
	public function detail() {
		$id = $this->request->post('id');
		!$id && $this->error('参数错误');
		
		$model = new \app\common\model\Goods();
		$goods = $model->where(['id' => $id, 'area' => ['in', [$this->area, $model::AREA_NULL]]])->find();
		!$goods && $this->error('商品不存在');
		
		$goods['specs'] = Db::name('goodsSpec')
			->where(['goods_id' => $id, 'status' => GoodsSpec::STATUS_Y])
			->field('id, name, price, store_count, image')
			->select();
		
		$goods['images'] = explode(',', $goods['images']);
		
		
		$goods['favorite_status'] = 0;
		$userId = $this->auth->id;
		if($userId) {
			Queue::addTask('addHistory', [
				'user_id' => $userId,
				'goods_id' => $id
			]);
			
			$favorite = \app\common\model\Favorite::get(['goods_id' => $id, 'status' => 1, 'user_id' => $userId]);
			$favorite && $goods['favorite_status'] = 1;
		}
		
		$groupWhere = [];
		$groupInfo = null;
		
		$myGroupOrder = Db::name('order')->alias('o')
			->join('orderGoods g', 'o.id = g.order_id', 'left')
			->where([
				'o.order_type' => \app\common\model\Order::TYPE_GROUP,
				'o.pay_time' => ['gt', 0],
				'o.group_id' => ['gt', 0],
				'o.user_id' => $userId,
				'g.goods_id' => $id,
			])
			->field('o.id, o.group_id, o.status')
			->order(['o.create_time' => 'desc'])
			->find();
		
		//优先显示我的在进行中的团
		$groupId = $this->request->post('group_id');
		if($groupId) {
			
			if($myGroupOrder && $myGroupOrder['status'] == \app\common\model\Order::STATUS_GROUP) {
				$groupId = $myGroupOrder['group_id'];
			}
		} else if($myGroupOrder) {
			$groupId = $myGroupOrder['group_id'];
		}
		
		if($groupId) {
			$groupWhere['o.group_id'] = $groupId;
			$groupInfo = Db::name('orderGroup')
				->where(['status' => ['neq', OrderGroup::STATUS_PAY], 'id' => $groupId])
				->find();
			if($groupInfo) {
				$users = Db::name('order')->alias('o')
					->join('user u', 'o.user_id = u.id', 'left')
					->where($groupWhere)
					->order(['o.create_time' => 'ASC'])
					->column('avatar', 'user_id');
				
				$groupInfo['joinStatus'] = in_array($userId, array_keys($users)) ? 1 : 0;
				$groupInfo['user'] = array_values($users);
				
			}
			
			
		}
		
		
			
		
		
		$goods['groupInfo'] = $groupInfo;
		$this->success('查询成功', $goods);
	}
	
	/**
	 * 浏览记录
	 */
	public function history() {
		$userId = $this->auth->id;
		$rows = Db::name('history')->alias('h')
			->join('goods g', 'h.goods_id = g.id', 'left')
			->where(['h.user_id' => $userId, 'h.status' => 1, 'g.status' => \app\common\model\Goods::STATUS_SALE])
			->order(['h.update_time' => 'desc'])
			->field('g.id, g.image, g.name')
			->limit(20)
			->select();
		$this->success('查询成功', $rows);
		
	}
}
